package com.oa.hyperion.api.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.hyperion.entity.Employee;
import com.oa.hyperion.exception.http.NotFoundException;
import com.oa.hyperion.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;




//使用RestController =controller+ ResponseBody
@Api(value="员工controller",tags={"员工操作接口"})
@RestController
@RequestMapping("/v1/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;



    @ApiOperation("获取所有员工信息")
    @GetMapping("getAll")
    public List<Employee> getAll(){
        return employeeService.findEmployeeWithPost();
    }


    @ApiOperation("获取同一部门的员工信息")
    @GetMapping("getSameList/{id}")
    //传入部门id
    public List<Employee> getSameList(@PathVariable Integer id){
        return employeeService.findAllEmployeeInSameDepartment(id);
    }

    @ApiOperation("获取所有实习员工信息")
    @GetMapping("getNewPersonList")
    public List<Employee> getNewPersonList(){
            QueryWrapper<Employee> wrapper=new QueryWrapper<>();
                    //找到实习期的员工
                wrapper.eq("status","s");
        return employeeService.list(wrapper);
    }

    @ApiOperation("获取所有不是部门领导的员工信息")
    @GetMapping("getNotLeaderList")
    public List<Employee> getNotLeaderList(){
        return employeeService.findEmployeeIsNotLeader();
    }


    @ApiOperation("分页查询所用员工")
    @GetMapping("getAllByPage")
    public Page<Employee> getAllByPage(Integer pageNum,Integer pageSize){
        //创建Page对象
        Page<Employee> employeePage = new Page<>(pageNum,pageSize);
        return employeeService.page(employeePage);
    }


    @ApiOperation("以名字，id，身份证来查找单个员工")
    @GetMapping("getOne")
    public List<Employee> getOneByCondition(@RequestBody Employee employee){
        QueryWrapper<Employee> wrapper=new QueryWrapper<>();
            //以id来查找
            if(employee.getId()!=null){
            wrapper.eq("id",employee.getId());
        }
            //以name来查找
        if(employee.getName()!=null){
            wrapper.eq("name",employee.getName());
        }
            //以身份证来查找
        if(employee.getIdentity()!=null){
            wrapper.eq("identity",employee.getIdentity());
        }

        List<Employee> e= employeeService.list(wrapper);
        if(e==null){
            throw new NotFoundException(404);
        }
            return e;
    }



    @ApiOperation("添加员工")
    @PostMapping("add")
    public String addEmployee(@RequestBody Employee employee){
            //设置初始化状态和入职时间
        employee.setStatus("s");
            //todo 时间可能需要修改格式
        employee.setJoinTime(new Date());

        if( employeeService.save(employee)) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 逻辑删除:员工
     *  修改status 状态为  离职="l"，让其不显示
     *  添加成功：返回true
     *      否则返回 false
     */
    @ApiOperation("逻辑删除:员工")
    @DeleteMapping("delete")
    public String deleteEmployee(Integer id){
        Employee employee=new Employee();
        employee.setId(id);
        employee.setStatus("l");
       boolean flag= employeeService.updateById(employee);
       if (flag){
           return "删除成功";
       }
        return "删除失败";

    }


    /**
     * 修改员工属性
     */
    @ApiOperation("修改员工属性")
    @PutMapping("update")
    public String updateEmployee(@RequestBody Employee employee){

        //获取要修改的对象
        Employee  emp=employeeService.getById(employee.getId());

        //有效性校验
        if(emp==null){
            throw new NotFoundException(404);
        }
        //修改传过来的功能
        if(employee.getId()!=null){
            emp.setId(employee.getId());
        }

        if(employee.getIdentity()!=null){
            emp.setIdentity(employee.getIdentity());
        }

        if(employee.getLeaveTime()!=null){
            emp.setLeaveTime(employee.getLeaveTime());
        }

        if(employee.getName()!=null){
            emp.setName(employee.getName());
        }

        if(employee.getGender()!=null){
            emp.setGender(employee.getGender());
        }

        if(employee.getJoinTime()!=null){
            emp.setJoinTime(employee.getJoinTime());
        }

        if(employee.getTelephone()!=null){
            emp.setTelephone(employee.getTelephone());
        }

        if(employee.getEmail()!=null){
            emp.setEmail(employee.getEmail());
        }

        if(employee.getStatus()!=null){
            emp.setStatus(employee.getStatus());
        }

        if(employee.getPost()!=null){
            emp.setPost(employee.getPost());
        }

        if( employeeService.updateById(emp)){
            return "修改成功";
        }
        return "修改失败";
    }


}
